﻿using jindong;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace EntityModel
{
    /// <summary>
    /// 数据上下文：负责链接数据库及配置
    /// </summary>
    public class ApiDbContext : DbContext
    {
        private readonly string? _ConnectionString;
        public ApiDbContext(DbContextOptions<ApiDbContext> opts, IConfiguration configuration) : base(opts)
        {
            _ConnectionString = configuration.GetConnectionString("ConnectionString");
        }

        /// <summary>
        /// 用户主表
        /// </summary>
        public DbSet<User> User { get; set; }
        /// <summary>
        /// 用户信息
        /// </summary>
        public DbSet<User_profile> User_profile { get; set; }

        /// <summary>
        /// 程序链接数据库的账户密码配置
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(_ConnectionString, new MySqlServerVersion(new Version(8, 0, 39)));
        }
        /// <summary>
        /// 创建数据库时设定库和表的字符集（可以省略，创建库以后，表默认继承库的设置）
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.UseCollation("utf8mb4_german2_ci").HasCharSet("utf8mb4");
        }
    }
}
